package demo_State;

public class State_Test4 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 =new Thread(()->{
            test();
        });
        Thread thread2 =new Thread(()->{
            test();
        });
        //启动线程之前记录一下线程状态
        System.out.println("thread1线程状态："+thread1.getState()+" TCB是否存活："+thread1.isAlive());
        System.out.println("thread2线程状态："+thread2.getState()+" TCB是否存活："+thread2.isAlive());
        System.out.println();
        thread1.start();
        thread2.start();
        //线程运行中记录线程状态
        Thread.sleep(1);//让打印线程慢一点防止抢占当前main线程抢占太快了
        System.out.println("thread1线程状态："+thread1.getState()+" TCB是否存活："+thread1.isAlive());
        System.out.println("thread2线程状态："+thread2.getState()+" TCB是否存活："+thread2.isAlive());
        System.out.println();
        //等待线程结束记录线程状态
        thread1.join();
        thread2.join();
        System.out.println("thread1线程状态："+thread1.getState()+" TCB是否存活："+thread1.isAlive());
        System.out.println("thread2线程状态："+thread2.getState()+" TCB是否存活："+thread2.isAlive());
        System.out.println();


    }
    //编写一个方法用来测试block
    public static  void test(){
        for (int i = 0; i < 1_0000_0000; i++) {
            synchronized (State_Test4.class){
                int m=0;
                for (int j = 0; j <10_0000_0000 ; j++) {
                    m++;
                }
            }
        }
    }
}